package jh.com.jhappbf.security;

import android.util.Base64;

import java.nio.charset.StandardCharsets;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;


public class DES {

    /**
     * DES加密
     *
     * @param data 名文
     * @param key  密钥
     * @return 密文
     */
    public static String encrypt(String data, String key) {
        try {

            if (key.length() > 8) {
                key = key.substring(0, 8);
            }

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 算法/模式/补码方式
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
//			BASE64Encoder endcoder = new BASE64Encoder();
            return android.util.Base64.encodeToString(result, Base64.NO_WRAP);
            //	return endcoder.encode(result);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * DES解密F
     *
     * @param secret 密文
     * @param key    密钥
     * @return 名文
     */
    public static String decrypt(String secret, String key) {
        try {
            key = key.substring(0, 8);
//			BASE64Decoder decoder = new BASE64Decoder();
            byte[] arr = android.util.Base64.decode(secret, Base64.NO_WRAP);
//			byte[] arr = decoder.decodeBuffer(secret);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec keySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
            SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = factory.generateSecret(keySpec);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(
                    key.getBytes(StandardCharsets.UTF_8));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] data = cipher.doFinal(arr);
            return new String(data);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 转换成16进制的字符串
     *
     * @param data
     * @return
     */
    public static String toHexString(byte[] data) {
        StringBuilder buffer = new StringBuilder(data.length * 2);
        String value;
        for (byte aData : data) {
            value = Integer.toHexString(aData & 0xff);
            if (value.length() < 2) {
                buffer.append("0");
            }
            buffer.append(value);
        }
        return buffer.toString();
    }

    /**
     * 转换成字节数组
     *
     * @param result
     * @return
     */
    public static byte[] convertHexString(String result) {
        byte[] data = new byte[result.length() * 2];
        String value;
        for (int index = 0; index < result.length() / 2; index++) {
            value = result.substring(index * 2, index * 2 + 2);
            int byteValue = Integer.parseInt(value, 16);
            data[index] = (byte) byteValue;
        }
        return data;
    }
}
